package org.mozhu.mboot.core.web.resource;

import lombok.extern.slf4j.Slf4j;
import org.mozhu.mboot.core.config.ApplicationConfiguration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

@Slf4j
public class ThemeWebResourceConfigurer extends BaseWebResourceConfigurer {
    // web.theme 指定主题静态资源的路径，如http://static.xxx.com/theme1/xxx
    private static final String PROPERTY_KEY_WEB_THEME_DOMAIN = "web.theme.domain";

    @Override
    public void addResourceHandler(ResourceHandlerRegistry registry) {
        // 因为Metronic模板主题的静态资源太大，不将它放在工程目录下的static中，避免更新代码过于缓慢
        // 可以放在本地磁盘中，映射下面的路径
        // 如果需要修改模板主题中的资源，则copy至src/main/resources/static/static目录中，进行修改，避免对原始资源修改，造成以后升级模板主题带来不必要的麻烦
        String webThemeLocation = ApplicationConfiguration.get().getProperty("web.theme.location");
        webThemeLocation = makeSureResourcePathEndswithSlash(webThemeLocation);
        if (webThemeLocation != null) {
            log.info("Mapping path[{}] to location[{}] ", "/theme/**", webThemeLocation);
            registry.addResourceHandler("/theme/**").addResourceLocations(webThemeLocation).setCachePeriod(86400);
        }

        String webTheme = ApplicationConfiguration.get().getProperty(PROPERTY_KEY_WEB_THEME_DOMAIN, "");
        webTheme = trimLastSlash(webTheme);
        servletContext.setAttribute(PROPERTY_KEY_WEB_THEME_DOMAIN, webTheme);
        log.info("Set [{}] to [{}]", PROPERTY_KEY_WEB_THEME_DOMAIN, webTheme);
    }

}
